package cn.com.zcode.exam.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import cn.com.zcode.core.dao.BaseMybatisDaoSupport;
import cn.com.zcode.exam.model.Answer;
import cn.com.zcode.exam.model.Examination;
import cn.com.zcode.exam.model.Option;
import cn.com.zcode.exam.model.PQConnector;
import cn.com.zcode.exam.model.Paper;
import cn.com.zcode.exam.model.Question;
import cn.com.zcode.exam.model.StaticInfo;
import cn.com.zcode.exam.model.Wrong;

public class ExamDaoImpl extends BaseMybatisDaoSupport implements ExamDao {
	@Override
	public List<Question> listQuestions(String pmp) {
		List<Question> list = this.list("listQuestionByType", pmp);
		return list;
	}

	@Override
	public Paper getPaperById(String pid) {
		Paper paper = this.get("getPaperById", pid);
		return paper;
	}

	public synchronized Question addQuestion(Question question) {
		this.add("addQuestion", question);
		String qid = question.getId();
		for (Option option : question.getOptions()) {
			option.setQuestionId(qid);
			this.add("addOption", option);
		}
		this.add("addPQConnector", new PQConnector(qid, question.getPaper()
				.getId()));
		return question;
	}

	@Override
	public List<Paper> listPapersInfo(String type, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("userId", userid);
		map.put("state", Paper.STATE_PUBLIC);
		List<Paper> list = this.list("listPapersInfo", map);
		return list;
	}

	@Override
	public Wrong getWrong(String userId, String questionId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		map.put("questionId", questionId);
		return this.get("getWrong", map);
	}

	@Override
	public Question getQuestion(String id) {
		return this.get("getQuestionById", id);
	}

	@Override
	public Paper getPaper(String type, String title, int state, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("title", title);
		map.put("state", state);
		map.put("userId", userid);
		return this.get("getPaper", map);

	}

	@Override
	public List<Question> listWrongQuestions(String type, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("userId", userId);
		return this.list("listWrongQuestions", map);
	}

	@Override
	public List<StaticInfo> listStaticInfoByType(String type) {
		return this.list("listStaticInfoByType", type);
	}

	@Override
	public List<Question> listLastExamQuestions(String eid) {
		return this.list("listLastExamQuestions", eid);
	}

	@Override
	public List<Question> listNewExamQuestions(String pid) {
		return this.list("listNewExamQuestions", pid);
	}

	@Override
	public List<Examination> listExamination(String key, String userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", key);
		map.put("userId", userid);
		map.put("state", Paper.STATE_PUBLIC);
		return this.list("listExamination", map);
	}

	@Override
	public List<Question> searchQuestions(String keyword, String userId) {
		keyword = keyword.trim();
		String kparm = "%" + keyword + "%";
		Map<String, Object> map = new HashMap<String, Object>();
		if (Pattern.matches("^\\d+$", keyword)) {
			int id = Integer.parseInt(keyword);
			map.put("no", id);
		}
		map.put("userId", userId);
		map.put("kparm", kparm);
		map.put("state_error", Paper.STATE_WRONG);
		map.put("state_public", Paper.STATE_PUBLIC);
		return this.list("searchQuestions", map);
	}

	@Override
	public void saveFinishExamination(String eid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("endTime", new Date());
		map.put("eid", eid);
		this.update("saveFinishExamination", map);

	}

	@Override
	public Paper addPaper(Paper paper) {
		return this.add("addPaper", paper);
	}

	@Override
	public Answer addAnswer(Answer answer) {
		return this.add("addAnswer", answer);
	}

	@Override
	public Examination addExamination(Examination examination) {
		return this.add("addExamination", examination);
	}

	@Override
	public Wrong addWrong(Wrong wrong) {
		return this.add("addWrong", wrong);
	}

	@Override
	public void updateQuestion(Question question) {
		this.update("updateQuestion", question);
		List<Option> lists = question.getOptions();
		for (Option option : lists) {
			option.setQuestionId(question.getId());
			this.update("updateOption", option);
		}
	}

}
